package com.windrift.android.math;

import java.util.LinkedList;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.windrift.android.math.model.Formula;
import com.windrift.android.math.model.Operator;
import com.windrift.android.math.model.Utils;

public class MainActivity extends Activity {

	private static final int MIN = 0;
	private static final int HISTORY_TO_KEEP = 10;
	private int nextPosToInsert = 0;
	private TextView formulaView;
	private LinkedList<Formula> history = new LinkedList<Formula>();

	private Button startButton;
	private Button resultButton;
	private Button prevButton;
	private Button nextButton;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		formulaView = (TextView) findViewById(R.id.formula);
		startButton = (Button) findViewById(R.id.button_start);
		resultButton = (Button) findViewById(R.id.button_result);
		prevButton = (Button) findViewById(R.id.button_prev);
		nextButton = (Button) findViewById(R.id.button_next);
		resultButton.setEnabled(false);
		prevButton.setEnabled(false);
		nextButton.setEnabled(false);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		//refer to onOptionsItemSelected
//		MenuItem setting = (MenuItem) menu.findItem(R.id.menu_settings);
//		setting.setOnMenuItemClickListener(new OnMenuItemClickListener() {
//
//			@Override
//			public boolean onMenuItemClick(MenuItem arg0) {
//				Intent intent = new Intent(MainActivity.this,
//						SettingsActivity.class);
//				startActivity(intent);
//				return true;
//			}
//		});

		return true;
	}

	 @Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_settings: {
			showSettings();
			return true;
		}
		default:
			return super.onOptionsItemSelected(item);
		}

	}

	public void onStartButtonClick(View view) {
        SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
        int maxValue = Integer.parseInt(pref.getString("max_value", "10"));
//        int maxValue = 12;
		int firstNumber = Utils.getRandom(MIN, maxValue);
		int secondNumber = Utils.getRandom(MIN, maxValue - firstNumber);
		Formula add = new Formula(Operator.ADD, firstNumber, secondNumber);
		if (history.size() == HISTORY_TO_KEEP) {
			history.removeFirst();
			nextPosToInsert--;
		}

		// always add the new one to last
		nextPosToInsert = history.size();

		history.add(nextPosToInsert, add);
		nextPosToInsert++;
		showFormula(add);
		resultButton.setEnabled(true);
		setPrevNextButtonStatus();

	}

	private void setPrevNextButtonStatus() {
		if (nextPosToInsert > 1)
			prevButton.setEnabled(true);
		else
			prevButton.setEnabled(false);

		if (history.size() > nextPosToInsert)
			nextButton.setEnabled(true);
		else
			nextButton.setEnabled(false);
	}

	public void onResultButtonClick(View view) {
		formulaView.append(Integer.toString(history.get(nextPosToInsert - 1)
				.getResult()));
		resultButton.setEnabled(false);
	}

	public void onPrevButtonClick(View view) {
		if (nextPosToInsert > 1) {
			nextPosToInsert--;
			showFormula(history.get(nextPosToInsert - 1));
			setPrevNextButtonStatus();
			resultButton.setEnabled(true);
		}
	}

	public void onNextButtonClick(View view) {
		if (history.size() > nextPosToInsert) {
			nextPosToInsert++;
			showFormula(history.get(nextPosToInsert - 1));
			setPrevNextButtonStatus();
			resultButton.setEnabled(true);
		}

	}

	private void showSettings() {
		Intent intent = new Intent(MainActivity.this,
				SettingsActivity.class);
		startActivity(intent);
	}

	private void showFormula(Formula f) {
		formulaView.setText("");
		formulaView.append(Integer.toString(f.getFirstNumber()));
		formulaView.append(" " + f.getOperator().toString() + " ");
		formulaView.append(Integer.toString(f.getSecondNumber()));
		formulaView.append(" = ");

	}

}
